package com.rrd.hive.udf.phonebook;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.metadata.HiveException;

import java.util.HashMap;
import java.util.Map;

import static com.rrd.hive.udf.phonebook.CvPhoneBookGetTags.getInputTags;

@Description(
        name = "cv_phonebook_parse",
        value = "_FUNC_(name, remark, company) - Get name_tags, remark_tags, company_tags",
        extended = "Example:\n> SELECT _FUNC_( 'name', 'remark', 'company');\n "
)


public class CvPhoneBookUDF extends UDF {

    public static Configuration config() {
        System.setProperty("HADOOP_USER_NAME", "hdfs");
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        return conf;
    }

    public Map<String, String> evaluate(String name, String remark, String company) throws HiveException {
        Map<String, String> resMap = new HashMap<>();

        resMap.put("name_tags", getInputTags(name));
        resMap.put("remark_tags", getInputTags(remark));
        resMap.put("company_tags", getInputTags(company));
        return resMap;
    }

    public static void main(String[] args) throws HiveException {
        System.out.println(new CvPhoneBookUDF().evaluate("123", "456", "789"));
    }

}
